package com.youyiwen.Controller;

import com.youyiwen.Bean.User;
import com.youyiwen.Mapper.NoticeMapper;
import com.youyiwen.Service.NoticeService;
import com.youyiwen.dto.NoticeDTO;
import com.youyiwen.dto.PageInitDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: zhaoyang
 * @Date: 2021/04/06
 */
@Controller
public class NoticeController {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private NoticeService noticeService;

    @GetMapping("/userCenter/notices")
    public String notice(@RequestParam(name = "page",defaultValue = "1") Integer page,
                         @RequestParam(name = "size",defaultValue = "6") Integer size,
                         HttpSession session, Model model) {
        User user = (User) session.getAttribute("loginUser");
        if (user == null){
            model.addAttribute("msg","请先登录");
            return "login";
        }
        String userName = user.getUserName();
        PageInitDTO<NoticeDTO> pageInitDTO = noticeService.showNotices(page,size,userName);
        model.addAttribute("notices", pageInitDTO);
        return "notices";
    }

    @GetMapping("/changeRead/question/{id}/{noticeId}/{parentId}")
    public String changeRead(@PathVariable("noticeId") String noticeId){
        noticeMapper.updateRead(noticeId);
        return "redirect:/question/{id}#{parentId}";
    }


    @GetMapping("/changeRead/notices/{userName}")
    public String allNoticesRead(@PathVariable String userName){
        noticeMapper.allNoticesRead(userName);
        return "redirect:/userCenter/notices";
    }

    @GetMapping("/delete/notices/{userName}")
    public String deleteAllNotices(@PathVariable String userName){
         noticeMapper.deleteNotice(userName);
         return "redirect:/userCenter/notices";
    }
}
